package com.example.customview.expandtextview.util;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.TextView;

/**
 * @author: Mr.You
 * @create: 2022-10-22 09:39
 * @description:
 **/
public class TextViewUtils {
    /**
     * 用于textview 展示超出两行显示...并且在后面跟上图标
     *@param
     *@param icon 末尾图标
     *@param text 展示的内容
     *@param title 弹窗上需要展示的标题 没有可以不传
     *@param context 上下文
     */
    public void setTextLineWithIcon(final TextView textView, final int icon, final String text, final String title
            , final Context context) {
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.post(new Runnable() {
            @Override
            public void run() {
                Layout layout = textView.getLayout();
                int lineCount = layout.getLineCount();
                if (lineCount == 0) {
                    return;
                }

                String result = "";
                int lineEnd = 0;
                if (lineCount == 1) {
                    lineEnd = layout.getLineEnd(0);
                } else if (lineCount >= 2) {
                    //当行数大于等于两行时取第二行末尾的下标 如果需要在第三行接图标可以用layout.getLineEnd(2); 这儿注意判断行数是否大于三行 不然这样取会崩溃
                    lineEnd = layout.getLineEnd(1);
                }
                if (lineEnd > 2) {
                    result = text.substring(0, lineEnd - 2);
                }
                SpannableStringBuilder spannableString = new SpannableStringBuilder();
                spannableString.clear();
                spannableString.append(result).append("...  ");
                ClickableSpan clickableSpan = new ClickableSpan() {
                    @Override
                    public void onClick(View view) {
                        //图标点击事件 根据需求不同 逻辑不同

                    }

                    @Override
                    public void updateDrawState(@androidx.annotation.NonNull TextPaint ds) {
                        super.updateDrawState(ds);
                       // ds.setColor(BaseApplication.getContext().getResources().getColor(android.R.color.transparent));
                    }
                };
                //也可以这样
                Drawable drawable = textView.getContext().getResources().getDrawable(icon);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                ImageSpan imageSpan1 = new ImageSpan(drawable);
                spannableString.setSpan(imageSpan1, spannableString.length() - 2, spannableString.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(clickableSpan, spannableString.length() - 2, spannableString.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                textView.setText(spannableString);
                textView.setHighlightColor(textView.getContext().getResources().getColor(android.R.color.transparent));
            }
        });
    }

}
